/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: ResourceController.java
 * Author: YuXingZh
 * Date: 29/5/2024 下午3:26
 * Description:
 * 资源管理Controller层
 * History:
 * Date          Author   Version  Desc
 * 2024-01-01    bosssoft  1.0.0   initialize this file
 */
package com.bosssoft.g1.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bosssoft.g1.common.annotation.ApiLog;
import com.bosssoft.g1.common.data.dto.IdVersion;
import com.bosssoft.g1.common.data.vo.CommonResponse;
import com.bosssoft.g1.common.exception.parse.BusinessException;
import com.bosssoft.g1.api.ResourceApi;
import com.bosssoft.g1.dto.PageDTO;
import com.bosssoft.g1.dto.ResourceDTO;
import com.bosssoft.g1.dto.UserDTO;
import com.bosssoft.g1.entity.Node;
import com.bosssoft.g1.entity.Resource;
import com.bosssoft.g1.service.ResourceService;
import com.bosssoft.g1.utils.CommonResponseUtils;
import com.bosssoft.g1.vo.IdNameVO;
import com.bosssoft.g1.vo.ResourceVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @className: ResourceController
 * @description: 解决什么问题：
 * 演示线程池的正确使用
 * 主要方法和数据成员：
 * methodxx 使用 ThreadPoolExecutor创建线程进行任务操作
 * 注意事项：
 * 例如：该类需要从容器获取不可以new
 * @author: YuXingZh
 * @date: 29/5/2024下午3:26
 * @since 1.0
 **/

@RestController
public class ResourceController implements ResourceApi {

    @Autowired
    ResourceService resourceService;

    @Override
    public CommonResponse<PageInfo<ResourceDTO>> getAllRes(PageDTO pageDTO) {
        PageInfo<ResourceDTO> resources=resourceService.getAllRes(pageDTO);
        if(resources != null){
            return CommonResponseUtils.success(resources);
        }else{
            return CommonResponseUtils.failed("查询用户信息失败");
        }
    }

    @Override
    public CommonResponse<List<IdNameVO>> getAllSubSystem() {
        List<IdNameVO> subSystems=resourceService.getAllSubSystem();
        if(subSystems != null){
            return CommonResponseUtils.success(subSystems);
        }else{
            return CommonResponseUtils.failed("查询子系统信息失败");
        }
    }

    /**
     * 界面调用接口增加资源
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @return true成功 false失败
     */
    @Override
    public CommonResponse<PageInfo<ResourceVO>> resourceName(String name, Integer pageNum, Integer pageSize) {
        PageInfo<ResourceVO> resourcePageInfo = resourceService.queryByName(name, pageNum, pageSize);
        return CommonResponseUtils.success(resourcePageInfo);
    }

    /**
     * 删除资源
     *
     * @param id 记录id
     * @return true 成功 false失败
     */
    @Override
    public CommonResponse<PageInfo<ResourceVO>> resourceTree(Long id, Integer pageNum, Integer pageSize) {
        PageInfo<ResourceVO> resourcePageInfo = resourceService.queryById(id, pageNum, pageSize);
        return CommonResponseUtils.success(resourcePageInfo);
    }

    /**
     * 增加资源
     *
     * @param resourceDTO 资源DTO
     * @return true 成功 false失败
     */
    @Override
    public CommonResponse<Boolean> addResource(ResourceDTO resourceDTO) {
        boolean flag = resourceService.addResource(resourceDTO);
        if (flag) {
            return CommonResponseUtils.success();
        } else {
            return CommonResponseUtils.failed("增加资源信息失败");
        }
    }

    /**
     * 通过 id 查询资源
     *
     * @param id 当前行id
     * @return 资源数据
     */
    @Override
    public CommonResponse<ResourceVO> findById(Long id) {
        ResourceVO resourceVO = resourceService.findById(id);
        if (!ObjectUtil.isNull(resourceVO)) {
            return CommonResponseUtils.success(resourceVO);
        } else {
            return CommonResponseUtils.failed("增加资源信息失败");
        }
    }

    /**
     * 资源修改结果保存
     *
     * @param resourceDTO 资源信息包含 id
     * @return true成功 false失败
     */
    @Override
    public CommonResponse<Boolean> updateResource(ResourceDTO resourceDTO) {
        int i = resourceService.updateResource(resourceDTO);
        if (i == 1) {
            return CommonResponseUtils.success();
        } else if (i == 0) {
            return CommonResponseUtils.failed("资源已修改");
        } else {
            return CommonResponseUtils.failed("资源修改失败");
        }
    }

    /**
     * 删除单个资源
     *
     * @param id
     * @return
     */
    @Override
    public CommonResponse<Boolean> deleteResource(@RequestParam Long id) {
        Boolean result = resourceService.deleteResource(id);
        return CommonResponseUtils.booleanResult(result);
    }

    @ApiLog
    @Override
    public CommonResponse<Boolean> batchDeleteResource(@NotEmpty @RequestBody List<ResourceDTO> resourceDTOS){
        try {
            Boolean result = resourceService.batchDeleteResource(resourceDTOS);
            return CommonResponseUtils.booleanResult(result);
        } catch (BusinessException e) {
            return CommonResponseUtils.failedWithMsg("删除失败：",e.getMessage());
        }
    }
    /**
     * 显示资源树
     * @param id
     * @return
     */
    @Override
    public CommonResponse<List<Node>> queryResourceByTenantId(@RequestParam(value = "id") @NotNull Long id) {
        List<Node> nodeList = resourceService.queryResourceByTenantId(id);
        if(nodeList != null){
            return CommonResponseUtils.success("显示资源树成功！",nodeList);
        }else{
            return CommonResponseUtils.failed("显示资源树失败！");
        }
    }

    /**
     * 根据子系统id查询资源树
     * @param id
     * @return
     */
    @Override
    public CommonResponse<List<Node>> queryResourceBySusId(@RequestParam(value = "id") @NotNull Long id){
        List<Node> nodeList = resourceService.queryResourceBySusId(id);
        if(nodeList != null){
            return CommonResponseUtils.success("显示资源树成功！",nodeList);
        }else{
            return CommonResponseUtils.failed("显示资源树失败！");
        }
    }
}
